package ace.cmp.spring.util.core;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * @author caspar
 * @date 2023/9/20 9:23
 */
@Slf4j
public final class SpringContextUtils {
  public static Boolean containsBean(DefaultListableBeanFactory beanFactory, Class beanClass) {
    List<BeanDefinition> beanDefinitions =
        Arrays.stream(beanFactory.getBeanDefinitionNames())
            .map(beanName -> beanFactory.getBeanDefinition(beanName))
            .filter(
                beanDefinition ->
                    beanDefinition.getResolvableType().getRawClass().equals(beanClass))
            .collect(Collectors.toList());

    return beanDefinitions.size() > 0;
  }
}
